package cn.mineserv.video.videoordersystem.config;

import org.springframework.http.HttpStatus;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.NoHandlerFoundException;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.logging.Logger;

/**
 * 全局异常处理
 * @author Y_YuanZhouLv
 */
//@RestControllerAdvice
public class GlobalException {
    private static final Logger logger = Logger.getLogger(GlobalException.class.getName());
    /**
     * 非法请求异常
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = IllegalArgumentException.class)
    public void badRequestException(IllegalArgumentException exception) {
        logger.fine("非法请求，请求失败！"+ Arrays.deepToString(exception.getStackTrace()));
    }

    /**
     * 资源不存在异常
     */
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(value = NoHandlerFoundException.class)
    public void errorHandler(NoHandlerFoundException exception) {
        logger.info("资源不存在"+ Arrays.deepToString(exception.getStackTrace()));
    }

    /**
     * 请求方式错误
     */
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public void errorHandler(HttpRequestMethodNotSupportedException exception) {
        logger.fine("请求方式错误"+ Arrays.deepToString(exception.getStackTrace()));
    }

    /**
     * 请求参数错误
     */
    @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    public void errorHandler(HttpMediaTypeNotSupportedException exception) {
        logger.fine("请求参数错误"+ Arrays.deepToString(exception.getStackTrace()));
    }

    /**
     * 数据库异常
     */
    @ExceptionHandler(SQLException.class)
    public void mySQLException(SQLException exception) {
        logger.info("数据库异常，操作失败！"+ Arrays.deepToString(exception.getStackTrace()));
    }

    /**
     * 服务器异常
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(value = Exception.class)
    public void errorHandler(Exception exception) {
        logger.info("服务器异常"+ Arrays.deepToString(exception.getStackTrace()));
    }


}
